# -*- coding: utf-8-*-
# Historical data distribution in pivot point strategy
# By Jun Lv,  Jun.Alex.Lv@gmail.com
# This software is not placed into the public domain
# Revision date: May 29 2014
# Version: 1.0
global listSS3toSS2,listSS2toSS1,listSS1toPP,listPPtoRR1,listRR1toRR2,listRR2toRR3;
listSS3toSS2=listSS2toSS1=listSS1toPP=listPPtoRR1=listRR1toRR2=listRR2toRR3=[];
global step;
step=0.1;

def pivotpoint(high,low,close):
    PP=(high+low+close)/3;                      
    RR1=PP*2-low;
    RR2=PP+high-low;
    RR3=RR2+high-low;
    SS1=PP*2-high;
    SS2=PP-high+low;
    SS3=SS2-high+low;
    return(PP,RR1,RR2,RR3,SS1,SS2,SS3)

def statisticInter(c,PP,RR1,RR2,RR3,SS1,SS2,SS3):
    #initialnize count value for all interval distribution    
    belowSS3count=0;
    SS3toSS2count=0;
    SS2toSS1count=0;
    SS1toPPcount=0;
    PPtoRR1count=0;
    RR1toRR2count=0;
    RR2toRR3count=0;
    aboveRR3count=0;
    totalcount=0;
    global listSS3toSS2,listSS2toSS1,listSS1toPP,listPPtoRR1,listRR1toRR2,listRR2toRR3;
    #listSS3toSS2=listSS2toSS1=listSS1toPP=listPPtoRR1=listRR1toRR2=listRR2toRR3=[];
    step=0.05;
    n=int(1/step);
    

    
    func = lambda x,y : x+y;
    
    if c<=SS3:
        aboveRR3count=aboveRR3count+1;
        
    if c>SS3 and c<=SS2:
        SS3toSS2count=SS3toSS2count+1;
        tmplist=stasticIntra(c,SS2,SS3);
        listSS3toSS2=map(func,listSS3toSS2,tmplist);            
        
    if c>SS2 and c<=SS1:
        SS2toSS1count=SS2toSS1count+1;
        tmplist=stasticIntra(c,SS1,SS2);
        listSS2toSS1=map(func,listSS2toSS1,tmplist);           
        
    if c>SS1 and c<=PP:
        SS1toPPcount=SS1toPPcount+1;
        tmplist=stasticIntra(c,PP,SS1);
        listSS1toPP=map(func,listSS1toPP,tmplist);               
        
    if c>PP and c<=RR1:
        PPtoRR1count= PPtoRR1count+1;
        tmplist=stasticIntra(c,RR1,PP);
        listPPtoRR1=map(func,listPPtoRR1,tmplist);         
         
    if c>RR1 and c<=RR2:
        RR1toRR2count=RR1toRR2count+1;
        tmplist=stasticIntra(c,RR2,RR1);
        listRR1toRR2=map(func,listRR1toRR2,tmplist);              
        
    if c>RR2 and c<=RR3:
        RR2toRR3count=RR2toRR3count+1;
        tmplist=stasticIntra(c,RR3,RR2);
        listRR2toRR3=map(func,listRR2toRR3,tmplist);     
        
    if c>RR3:
        aboveRR3count=aboveRR3count+1;
    
    totalcount=totalcount+belowSS3count+SS3toSS2count+SS2toSS1count+SS1toPPcount+PPtoRR1count+RR1toRR2count+RR2toRR3count+aboveRR3count ; 
    return(belowSS3count,SS3toSS2count,SS2toSS1count,SS1toPPcount,PPtoRR1count,RR1toRR2count,RR2toRR3count,aboveRR3count,totalcount) 
   
def stasticIntra(c,up,down):     
    #intraline=[];
    global step;
    intracount=[];
    n=int(1/step);
    
    #init count & edge point value
    for i in range(0,n):
        #intraline.append(down+(up-down)*step*i);
        intracount.append(0); 
        
    i=int((c-down)/((up-down)/n));
    if(i==n):
        i=n-1;
    intracount[i]=intracount[i]+1; 
    return intracount    

import re

high=-999999;
low=999999;
firstdate='2010-04-16';
lastdate='2010-04-16';
belowSS3count=0;
SS3toSS2count=0;
SS2toSS1count=0;
SS1toPPcount=0;
PPtoRR1count=0;
RR1toRR2count=0;
RR2toRR3count=0;
aboveRR3count=0;
totalcount=0;

global step;
n=int(1/step);
for i in range(0,n):
    #intraline.append(down+(up-down)*step*i);
    listRR2toRR3.append(0);     
    listSS3toSS2=listSS2toSS1=listSS1toPP=listPPtoRR1=listRR1toRR2=listRR2toRR3;


with open ('D:\Users\lv\Data\cffex\cffex.csv','rb')  as fp:
    for line in fp:
        data=re.split(",",line);
        
        if(re.match('\d{4}-\d{2}-\d{2}',data[0])):
            mdate=data[0];
            if mdate!=firstdate and mdate!=lastdate:
                PP,RR1,RR2,RR3,SS1,SS2,SS3=pivotpoint(high,low,close);
                high=-999999;
                low=999999;                       
            
                   
            mtime=data[1];
            mhigh=float(data[3]);
            mlow=float(data[4]);
            mclose=float(data[5]);
            #print date,time,high,low,close; 
            if mdate!=firstdate:
                tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8,tmp9=statisticInter(mclose,PP,RR1,RR2,RR3,SS1,SS2,SS3);       
                belowSS3count=belowSS3count+tmp1;
                SS3toSS2count=SS3toSS2count+tmp2;
                SS2toSS1count=SS2toSS1count+tmp3;
                SS1toPPcount=SS1toPPcount+tmp4;
                PPtoRR1count=PPtoRR1count+tmp5;
                RR1toRR2count=RR1toRR2count+tmp6;
                RR2toRR3count=RR2toRR3count+tmp7;
                aboveRR3count=aboveRR3count+tmp8;
                totalcount=totalcount+tmp9;
                   
            if mhigh>=high:
                high=mhigh;
                
            if mlow<=low:
                low=mlow; 
         
            lastdate=mdate;
            close=mclose;
            
#print ("AboveRR3\tRR3toRR2\tRR2toRR1\tRR1toPP\tPPtoSS1\tSS1toSS2\tSS2toSS3\tbelowSS3");
print ("BelowSS3\tSS3toSS2\tSS2toSS1\tSS1toPP\tPPtoRR1\tRR1toRR2\tRR2toRR3\tAboveRR3");
print ("\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s") %(belowSS3count,SS3toSS2count,SS2toSS1count,SS1toPPcount,PPtoRR1count,RR1toRR2count,RR2toRR3count,aboveRR3count);
print ("total count of K Bar:%s") %totalcount;
print ("\tSS3toSS2\tSS2toSS1\tSS1toPP\tPPtoRR1\tRR1toRR2\tRR2toRR3");
for i in range(0,n):
    print("%s\t%s\t%s\t%s\t%s\t%s\t%s") %(i,listSS3toSS2[i],listSS2toSS1[i],listSS1toPP[i],listPPtoRR1[i],listRR1toRR2[i],listRR2toRR3[i]);
    
    